package com.sgb.member.weight;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.animation.Animation;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.interpolator.view.animation.FastOutSlowInInterpolator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lxj.xpopup.animator.PopupAnimator;
import com.lxj.xpopup.core.AttachPopupView;
import com.lxj.xpopup.impl.PartShadowPopupView;
import com.sgb.member.R;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.view.selection.MaxHeightRecyclerView;

import java.util.List;


@SuppressLint("ViewConstructor")
public class TenderTypePopup extends PartShadowPopupView {

    private int mCurPos = 0;
    private List<CommonPopEntity> mData;
    private BaseRecycleAdapter<CommonPopEntity> mAdapter;
    private final SimpleResult<CommonPopEntity> mSimpleResult;

    public TenderTypePopup(@NonNull Context context,
                           List<CommonPopEntity> mData,
                           int mCurPos,
                           SimpleResult<CommonPopEntity> mSimpleResult) {
        super(context);
        this.mData = mData;
        this.mCurPos = mCurPos;
        this.mSimpleResult = mSimpleResult;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popwindow_common;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        MaxHeightRecyclerView recyclerView = findViewById(R.id.goods_type_list);
        findViewById(R.id.bg_pop_gray_layout).setOnClickListener(view -> dismiss());
        findViewById(R.id.common_layout).setBackgroundResource(R.color.color_white);

        int height = dip2px(45);
        recyclerView.setMaxHeight(height * 5 + height / 2);
        updateChildRecycler(recyclerView);
    }

    public int dip2px(float dipValue) {
        DisplayMetrics dm = getContext().getApplicationContext().getResources().getDisplayMetrics();
        return (int) (dipValue * dm.density + 0.5f);
    }

    public void updateChildRecycler(RecyclerView recyclerView) {
        mAdapter = new BaseRecycleAdapter<CommonPopEntity>(getContext(), R.layout.popwindow_common_item, mData) {
            @Override
            public void convert(ItemViewHolder holder, CommonPopEntity entity) {
                RelativeLayout relativeLayout = holder.itemView.findViewById(R.id.item_bg);
                TextView textView = holder.itemView.findViewById(R.id.tv_name);
                textView.setText(entity.getPickerViewText());
                if (holder.getItem().isSelected()) {
                    relativeLayout.setBackground(null);
                    textView.setBackgroundResource(R.drawable.shape_check_txt);
                    textView.setTextColor(holder.itemView.getContext().getResources().getColor(R.color.color_main));
                } else {
                    relativeLayout.setBackground(null);
                    textView.setBackgroundResource(R.drawable.shape_uncheck_txt);
                    textView.setTextColor(holder.itemView.getContext().getResources().getColor(R.color.color_tab_two));
                }
            }
        };
        mAdapter.setItemSelect(true);
        mAdapter.setDefItem(mCurPos);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 4));
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClick(new AdapterOnItemClick<CommonPopEntity>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onItemClick(CommonPopEntity entity, int position) {
                mCurPos = position;
                dismiss();
                if (mSimpleResult != null) {
                    mSimpleResult.onResult(entity, position);
                }
            }
        });
    }
}
